#pragma once

#define INITGUID
#include "pimstore.h"
#undef INITGUID
#include "MzString.h"


#ifdef MZFC_EXPORTS
#define MZFC_API __declspec(dllexport)
#else
#define MZFC_API __declspec(dllimport)
#endif

struct MZFC_API  LabelContent
{
	DWORD dwLabelID;
	CMzString strContent;
};

struct MZFC_API stNameInfo
{
	CMzString strFirstName;
	CMzString strLastName;
	CMzString strCompanyName;
	CMzString strNamePinYin;
};

struct MZFC_API stAddreddInfo
{
	CMzString strStreet;
	CMzString strPostCode;
	CMzString strCity;
	CMzString strProvince;
	CMzString strContry;
	DWORD dwLabelID;
};

class MZFC_API ContactContentCach  
{
public:
	CMzString strPhotoPath;				
	struct stNameInfo ifName;
	struct LabelContent vPhoneNumber[10];
	DWORD cPhoneNumber;
	CMzString strRing;
	struct LabelContent vEmail[3];
	DWORD cEmail;
	struct LabelContent vWebPage[1];
	DWORD cWebPage;
	struct stAddreddInfo vAddress[3];
	DWORD cAddress;
	CMzString strNote;		
	CEOID oid;
public:
	ContactContentCach()
	{
		cPhoneNumber = 0;
		cEmail = 0;
		cWebPage = 0;
		cAddress = 0;
		oid = 0;
	}

	bool AddPhoneNumber(LPCWSTR str, DWORD dwLabelID)
	{
		if (cPhoneNumber < 10)
		{
			vPhoneNumber[cPhoneNumber].strContent = str;
			vPhoneNumber[cPhoneNumber].dwLabelID = dwLabelID;
			cPhoneNumber ++;
			return true;
		}
		else
		{
			return false;
		}
	}

	bool ErasePhoneNumber(unsigned int index)
	{
		if (index < cPhoneNumber - 1)
		{			
			for (unsigned int i = index; i< cPhoneNumber - 1; i++)
			{
				vPhoneNumber[i].strContent = vPhoneNumber[i+1].strContent;
				vPhoneNumber[i].dwLabelID = vPhoneNumber[i+1].dwLabelID;
			}			
		}

		cPhoneNumber -- ;
		vPhoneNumber[cPhoneNumber].strContent.Empty();
		return true;
	}


	bool AddEmail(LPCWSTR str, DWORD dwLabelID)
	{
		if (cEmail < 3)
		{
			vEmail[cEmail].strContent = str;
			vEmail[cEmail].dwLabelID = dwLabelID;
			cEmail ++;
			return true;
		}
		else
		{
			return false;
		}
	}


	bool EraseEmail(unsigned int index)
	{
		if (index < cEmail - 1)
		{			
			for (unsigned int i = index; i< cEmail - 1; i++)
			{
				vEmail[i].strContent = vEmail[i+1].strContent;
				vEmail[i].dwLabelID = vEmail[i+1].dwLabelID;
			}			
		}

		cEmail -- ;
		vEmail[cEmail].strContent.Empty();
		return true;
	}

	bool AddWebPage(LPCWSTR str, DWORD dwLabelID)
	{
		if (cWebPage < 1)
		{
			vWebPage[cWebPage].strContent = str;
			vWebPage[cWebPage].dwLabelID = dwLabelID;
			cWebPage ++;
			return true;
		}
		else
		{
			return false;
		}
	}

	bool EraseWebPage(unsigned int index)
	{
		cWebPage -- ;
		vWebPage[cWebPage].strContent.Empty();	
		return true;
	}

	bool AddAddress(LPCWSTR strCity, LPCWSTR strContry,LPCWSTR strPostCode,LPCWSTR strProvince,LPCWSTR strStreet,DWORD dwLabelID)
	{
		if (cAddress < 3)
		{
			vAddress[cAddress].dwLabelID = dwLabelID;
			vAddress[cAddress].strCity = strCity;
			vAddress[cAddress].strContry = strContry;
			vAddress[cAddress].strPostCode = strPostCode;
			vAddress[cAddress].strProvince = strProvince;
			vAddress[cAddress].strStreet = strStreet;
			cWebPage ++;
			return true;
		}
		else
		{
			return false;
		}
	}

	bool EraseAddress(unsigned int index)
	{
		if (index < cAddress - 1)
		{			
			for (unsigned int i = index; i< cAddress - 1; i++)
			{
				vAddress[i].strCity = vAddress[i+1].strCity;
				vAddress[i].dwLabelID = vAddress[i+1].dwLabelID;
				vAddress[i].strContry = vAddress[i+1].strContry;
				vAddress[i].strPostCode = vAddress[i+1].strPostCode;
				vAddress[i].strProvince = vAddress[i+1].dwLabelID;
				vAddress[i].strStreet = vAddress[i+1].strStreet;
			}			
		}

		cAddress -- ;
		vAddress[cAddress].strCity.Empty();	
		vAddress[cAddress].strContry.Empty();
		vAddress[cAddress].strPostCode.Empty();
		vAddress[cAddress].strProvince.Empty();
		vAddress[cAddress].strStreet.Empty();
		return true;
	}

	bool IsCachEmpty(void)
	{		
		if (!ifName.strCompanyName.Length())
		{
			return false;
		}

		if (!ifName.strFirstName.Length())
		{
			return false;
		}

		if (!ifName.strLastName.Length())
		{
			return false;
		}
	

		if (!strNote.Length())
		{
			return false;
		}

		if (!strPhotoPath.Length())
		{
			return false;
		}
	

		if (!strRing.Length())
		{
			return false;
		}

		if (!cEmail)
		{
			return false;
		}

		if (!cPhoneNumber)
		{
			return false;
		}

		if (!cWebPage)
		{
			return false;
		}

		if (!cAddress)
		{
			return false;
		}

		return true;
	}
};

class MZFC_API PoomHelper
{
public:
	PoomHelper(void);

	~PoomHelper(void);

	static IPOutlookApp* GetPolApp();

	static IFolder* GetPolFolder();

	static ContactContentCach* GetContactCach(CEOID oid = 0);

	static ContactContentCach* GetContactCach(IContact * pContact);
	
	static void PoomHelper::GetContactCach(IContact * pContact,ContactContentCach& ccc);

	void ShutdownPoom();

	static bool GetNameFromPhone(CMzString& strPhoneNumber, CMzString& strName,DWORD* pdwLabel = NULL, CEOID * piOid = NULL);
	
	static void GetPhoneNumberVector(IContact * pContact,struct LabelContent* glc, DWORD* lpcPhoneNumber);

	static void GetEmailVector(IContact * pContact,struct LabelContent* glc, DWORD* lpcEmail);

	static void GetWebPageVector(IContact * pContact,struct LabelContent* glc, DWORD* lpcWebPage);

	static void GetAddressVector(IContact * pContact,struct stAddreddInfo* gai, DWORD* lpcAddress);

	static void GetNameInfo(IContact * pContact);

	static bool PutPhoneNumberVector(IContact * pContact);	

	static bool PutEmailVector(IContact * pContact);	

	static bool PutWebpageVector(IContact * pContact);

	static bool PutAddressVector(IContact * pContact);

	static bool SaveContactInfo2DB(CEOID oid = 0);

	static bool SaveNameInfo2DB(CEOID oid = 0);

	static bool SavePhoneNumber2DB(CEOID oid = 0);

	static bool SaveWebpage2DB(CEOID oid = 0);

	static bool SaveEmail2DB(CEOID oid = 0);

	static bool SaveAddress2DB(CEOID oid = 0);

	static bool SavePhotoPath2DB(CEOID oid = 0);

	static bool SaveRingPath2DB(CEOID oid = 0);

	static CMzString GetContactWholeName();

	static CMzString GetContactWholeName(ContactContentCach ccc);

	static bool DeleteContact(CEOID oid = 0);

	static void InitContactCach();
private:
	static IPOutlookApp * m_polApp;
	static IFolder* m_polFolder;
	static ContactContentCach* m_pCantactCach;

	static  void StoreLabelContent(BSTR& bstr, struct LabelContent* glc, DWORD index);
	static  CMzString GetNamePinyin();

	static void UpdatePhoneMap();
	static bool match(CMzString& str1 , CMzString& str2);
	static CEOID Find(CMzString& strPhoneNumber);
};
